package com.itcodai.config.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: ChenBin
 * @Date: 2018/7/16/0016 22:19
 */
@Component
@EnableScheduling
public class JmeterThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
    private static final Logger logger = LoggerFactory.getLogger(JmeterThreadPoolTaskExecutor.class);

    private static AtomicInteger atomicInteger = new AtomicInteger(0);

    @Scheduled(fixedRate = 8000) // 每隔5秒执行一次
    public void monitorThreadPool() {
        if(atomicInteger.incrementAndGet() % 2 != 0) {
            logger.info("atomicInteger2-{}", atomicInteger.get());
            return;
        }
        logger.info("监控线程状态2！");
        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
        if (null == threadPoolExecutor) {
            return;
        }
        logger.info("jmeter perfix[{}], taskCount[{}], completedTaskCount[{}], activeCount[{}], queueSize[{}]",
                this.getThreadNamePrefix(),
                threadPoolExecutor.getTaskCount(),
                threadPoolExecutor.getCompletedTaskCount(),
                threadPoolExecutor.getActiveCount(),
                threadPoolExecutor.getQueue().size());
    }

    private synchronized void showThreadPoolInfo(String prefix) {
        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
        if (null == threadPoolExecutor) {
            return;
        }
        // 将任务总数、已完成数、活跃线程数，队列大小都打印出来
        // 提交任务到线程池的时候，调用的是submit(Callable task)这个方法，
        // 当前已经提交了3个任务，完成了3个，当前有0个线程在处理任务，还剩0个任务在队列中等待
        logger.info("{}, {}, taskCount[{}], completedTaskCount[{}], activeCount[{}], queueSize[{}]",
                this.getThreadNamePrefix(),
                prefix,
                threadPoolExecutor.getTaskCount(),
                threadPoolExecutor.getCompletedTaskCount(),
                threadPoolExecutor.getActiveCount(),
                threadPoolExecutor.getQueue().size());
        // async-service-, 2. do submit,taskCount [2], completedTaskCount [0], activeCount [2], queueSize [0]
    }

    /*@Override
    public void execute(Runnable task) {
        showThreadPoolInfo("1. do execute");
        super.execute(task);
    }*/

    /*@Override
    public void execute(Runnable task, long startTimeout) {
        showThreadPoolInfo("2. do execute");
        super.execute(task, startTimeout);
    }*/

    /*@Override
    public Future<?> submit(Runnable task) {
        showThreadPoolInfo("1. do submit");
        return super.submit(task);
    }*/

    // 申请线程，执行任务
    /*@Override
    public <T> Future<T> submit(Callable<T> task) {
        showThreadPoolInfo("2. do submit");
        return super.submit(task);
    }*/

    /*@Override
    public ListenableFuture<?> submitListenable(Runnable task) {
        showThreadPoolInfo("1. do submitListenable");
        return super.submitListenable(task);
    }*/

   /* @Override
    public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
        showThreadPoolInfo("2. do submitListenable");
        return super.submitListenable(task);
    }*/
}